﻿/**
  通过js文件，写死了需要显示的项。
***/
$(function($){
 
    initComponentGrid();
	
	// initMeasureGrid('yc');
	// initMeasureGrid('yx');
	// initMeasureGrid('yk');
	// initMeasureGrid('yt');
	
	
	});
	
	/////////////**********基本设备表初始化***********/////////////
function initComponentGrid() {

   
    var source = {
        dataType : "csv",
        dataFields : [{
            name : "component_type",
            type : "number"
        }, // Possible values: 'string', 'date', 'number', 'float', 'int', 'bool'.
        {
            name : "component_name",
            type : "string",
            format : ""
        }, {
            name : "component_svginfo",
            type : "string"
        }, {
            name : "component_view",
            type : "string"
        }],
        url:"compoent_base.csv",
        id : "component_type",
                addRow: function (rowID, rowData, position, commit) {
                    // synchronize with the server - send insert command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    // you can pass additional argument to the commit callback which represents the new ID if it is generated from a DB.
                    commit(true);
                },
                updateRow: function (rowID, rowData, commit) {
                    // synchronize with the server - send update command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    commit(true);
                },
                deleteRow: function (rowID, commit) {
                    // synchronize with the server - send delete command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    commit(true);
                }

    };

    var dataAdapter = new $.jqx.dataAdapter(source, {
        loadComplete : function() {

        }
    });
    // create jqxTreeGrid.
    // var filterrenderer = function (row, column, cellvalue) {
    //     var value = cellvalue.filter1+cellvalue.filter1_arg+cellvalue.filter_n+cellvalue.filter2+cellvalue.filter2_arg;
    //    return '<span style="margin: 6px 3px; font-size: 12px; float: right; font-weight: bold;">' + cellvalue + '</span>';
    // }
  var table=  $("#component_base").jqxGrid({
        source : dataAdapter,
        // altRows: true,
        width : '99.5%',
        height : '580',
        altrows: true,
        editable: true,
        theme : theme,
		selectionMode: "singleRow" ,
	    ready: function()//设置被选择的项
		{  

		},

        columns : [{
            text : "设备编号",
            align : "center",
            dataField : "component_type",
             
            editable : true ,validation: function (cell, value) {
                    
                          return true;
                      }  
        }, {
            text : "设备名称",
            cellsAlign : "center",
            align : "center",
            dataField : "component_name",
            
			editable : true    ,validation: function (cell, value) {
                          
                          return true;
                      }            
        }, {
            text : "图元信息",
            cellsAlign : "center",
            align : "center",
            dataField : "component_svginfo",
             
			editable : true   ,validation: function (cell, value) {
                          
                          return true;
                      }           
        }, {
            text : "缩略图",
            cellsAlign : "center",
            align : "center",
            dataField : "Omponent_view",
         
			editable : true  ,
			cellsrenderer: function()//在render中设置需要显示的缩略图
			{
				return "n";
			}          
        }]

    }).on('cellvaluechanged', function(event) {

            var field = args.datafield;
            var rowindex = args.rowindex;
            var value = args.newvalue;
            if (!value)
                return;

            var row = table.jqxGrid('getrowdata', rowindex);
            var oldvalue = args.oldvalue;
            //设置变化值到缓存中
            if (!$(this).data("rowData"))
                $(this).data("rowData", {});
            var rowData = $(this).data("rowData");
            var item = rowData[row.uid];
            if (item) {
                //更新的，直接更新数据，更新对应字段的数据。
                //添加的 ，不更新操作，直接更新数据。
                //删除的，提示删除一条已经备删除的记录。
                if (item.handle == 'delete') {
                    console.warn("错误，更新一条需要删除的记录");
                } else {
                    item.val[field] = value;
                }
            } else {
                //没有就创建一条更新的数据
                item = {};
                item.handle = 'update';
                item.val = {};
                item.val[field] = value;
            }

            rowData[row.uid] = item; //更新到rowdata

            $(this).data("rowData", rowData);



        });;
	
//===========================================
$("#tool_add").on("click",function(){
	var commit = table.jqxGrid('addrow', null, {});
	});
	
$("#tool_delete").on("click",function(){
		  var selection = table.jqxGrid('getSelection');
		  if(selection.length>0)
		 var commit =  table.jqxGrid('deleterow', selection[0].uid);
});

}
